//
//  EditProjectController.m
//  SEOBox
//
//  Created by Syd on 10-3-1.
//  Copyright 2010 MusicPeace.ORG. All rights reserved.
//

#import "Configure.h"
#import "EditProjectController.h"
#import "ProjectsArrayController.h"
#import <BWToolkitFramework/BWToolkitFramework.h>
#import "ASIWebThumbnailGenerator.h"
#import "Project.h"

@implementation EditProjectController
@synthesize nameTextField;
@synthesize urlTextField;
@synthesize progressIndicator;
@synthesize project;
@synthesize projectInfo;
@synthesize projectsArrayController;
@synthesize managedObjectContext;

- (id)init {
    if (![super initWithWindowNibName:@"EditProject"]) return nil;
    return self;
}

- (void)dealloc {
    [nameTextField release];
    [urlTextField release];
    [progressIndicator release];
    [project release];
    [projectInfo release];
    [projectsArrayController release];
    [managedObjectContext release];
    [super dealloc];
}

- (void)windowDidLoad {
    //NSLog(@"New Project Window Loaded");
    if (!projectInfo) {
        projectInfo = [[NSMutableDictionary alloc] initWithCapacity:3];
    }
    [projectInfo setObject:project.name forKey:@"name"];
    [projectInfo setObject:project.url forKey:@"url"];
    [nameTextField bind:@"value" toObject:projectInfo withKeyPath:@"name"  options:nil];
    [urlTextField bind:@"value" toObject:projectInfo withKeyPath:@"url"  options:nil];
    [progressIndicator setHidden:YES];
}


- (void)windowWillClose:(NSNotification *)notification {
    NSLog(@"window will close");
    [[NSNotificationCenter defaultCenter] postNotificationName:kEditProjectWindowWillClose object:projectInfo];
}

- (IBAction)cancel:(id)sender {
    //project = nil;
    projectInfo = nil;
    [self close];
}

- (IBAction)save:(id)sender {
    NSArray *keys = [[NSArray alloc] initWithObjects:@"name", @"url", @"snapshot", nil];
    NSArray *objs = [[NSArray alloc] initWithObjects:[nameTextField stringValue], [urlTextField stringValue], @"", nil];
    if (!projectInfo) {
        projectInfo = [[NSMutableDictionary alloc] initWithCapacity:3];
    }
    projectInfo = [NSMutableDictionary dictionaryWithObjects:objs forKeys:keys];
    [keys release];
    [objs release];
    if ([self validateProject]) {
        project.name = [projectInfo objectForKey:@"name"];
        project.url = [projectInfo objectForKey:@"url"];
        project.snapshot = [projectInfo objectForKey:@"snapshot"];
        [self close];
    }
}

- (BOOL)validateProject
{
    if ([[projectInfo objectForKey:@"name"] length]<3) {
        NSRunAlertPanel(@"Error", @"Project name should not be less than 3 charaters", @"OK", nil, nil);
        return NO;
    }
    if ([projectsArrayController checkDuplicate:[projectInfo objectForKey:@"name"] url:[projectInfo objectForKey:@"url"]]) {
        NSRunAlertPanel(@"Error", @"Project has been existed!", @"OK", nil, nil);
        return NO;
    }
    [progressIndicator setHidden:NO];
    [progressIndicator startAnimation:self];
    ASIWebThumbnailGenerator *generator = [[[ASIWebThumbnailGenerator alloc] init] autorelease];
    [generator setUrl:[projectInfo objectForKey:@"url"]];
    [generator setPageSize:NSMakeSize(1000,0)];
    [generator setSourceSize:NSMakeSize(1000,822)];
    [generator setDestinationSize:NSMakeSize(292,240)];
    [generator start];
    
    NSImage *img = [generator image];
    NSBitmapImageRep *rep = [[img representations] objectAtIndex:0];
    NSData *imgData = [rep representationUsingType:NSPNGFileType properties:nil];
    [projectInfo setValue:imgData forKey:@"snapshot"];
    [progressIndicator stopAnimation:self];
    [progressIndicator setHidden:YES];
    return YES;
}
@end
